<?php

namespace App\Http\Controllers;

use App\Http\Model\Hint;
use App\Http\Model\OutJson;
use App\Http\Model\User;
use App\Http\Services\MsgServices;
use App\Http\Services\RoomMsgServices;
use App\Http\Services\RoomVoiceCheckServices;
use App\Http\Services\SystemServices;
use App\Http\Services\UserServices;
use App\Lib\rc;
use Illuminate\Support\Facades\Log;
use Illuminate\Http\Request;

class RoomVoiceCheckController extends BaseController
{
    protected $servicesRoomViceCheck;

    public function __construct()
    {
        $this->servicesRoomViceCheck = new RoomVoiceCheckServices();
    }


    public function loopStart()
    {
        do {
            if (!$this->checkRequest(false)) {
                break;
            }

            $this->callCommon($this->servicesRoomViceCheck, 'loopStart', $this->requestData);

        } while (false);

        $this->responseData();
    }


    public function loopStop()
    {
        do {
            if (!$this->checkRequest(false)) {
                break;
            }
            $this->callCommon($this->servicesRoomViceCheck, 'loopStop', $this->requestData);

        } while (false);

        $this->responseData();
    }

    public function callBack()
    {
        do {
            if (!$this->checkRequest(false)) {
                break;
            }
            $this->callCommon($this->servicesRoomViceCheck, 'callBack', $this->requestData, false);

            $roomId = $this->responseData['data']['room_id'] ?? null;
            $userId = $this->responseData['data']['user_id'] ?? null;
            $type = $this->responseData['data']['type'] ?? null;

            if (empty($type)) {
                break;
            }
            $roomMsgService = new RoomMsgServices();
            $userService = new UserServices();
            $userModel = new User();

            if (($type == 1 || $type == 4) && !empty($roomId) && !empty($userId)) {
                //封号
                $roomMsgService->command_user_out($roomId, $userId,3600);
                $tmpRoomId = null;
                sleep(2);

                $timeout = time() + 43200 * 2; //封号时长：24小时
                $userService->set_forbid_user($userId, $timeout, $tmpRoomId);

            } elseif (($type == 2 || $type == 5) && !empty($roomId) && !empty($userId)) {
                //警告
                $userInfo = $userModel->get_user($userId);
                $nickname = $userInfo['nickname'] ?? "";
                $uuid = $userInfo['user_uid'] ?? "";
                $msg = "系统检测到%s(ID:%s)的语音内容违规，将进行10分钟禁麦警告处理，请注意您的言行需符合平台绿色规范，若再次违规将进行封号处理。";
                $text = sprintf($msg, $nickname, $uuid);
                $roomMsgService->pronWarningMsg($roomId, $text, $userId);
            }
        } while (false);

        $this->responseData();
    }

    public function setConfig() {
        do {
            if (!$this->checkRequest(false)) {
                break;
            }
            $this->callCommon($this->servicesRoomViceCheck, 'setConfig', $this->requestData);

        } while (false);

        $this->responseData();
    }

    public function getConfig() {
        do {
            if (!$this->checkRequest(false)) {
                break;
            }
            $this->callCommon($this->servicesRoomViceCheck, 'getConfig', $this->requestData);

        } while (false);

        $this->responseData();
    }

    //修改语音审查记录
    public static function eidtVoiceCheckLogStatus(Request $request)
    {
        $id = $request->input('id') ?? 0;
        $status = $request->input('status') ?? 0;
        $roomVoice = New RoomVoiceCheckServices();
        $res = $roomVoice->updateVoiceCheckLog($id,$status);
        if($res){
            OutJson::outJsonResult(Hint::SUCCESS);
        }
        OutJson::outJsonResult(Hint::ERROR);
    }


    /**
     * 获取所有高频检测房间
     * @return mixed
     */
    public function getHighRooms(Request $request){
        $data = $this->servicesRoomViceCheck->getHighRooms();
        OutJson::outJsonResult(Hint::SUCCESS, $data);
    }


    /**
     * 添加高频检测房间
     * @return mixed
     */
    public function addHighRoom(Request $request){
        $roomId = $request->get('room_id');
        $this->servicesRoomViceCheck->addHighRoom($roomId);
        OutJson::outJsonNoData(Hint::SUCCESS);
    }

    /**
     * 删除高频检测房间
     * @return mixed
     */
    public function delHighRoom(Request $request){
        $roomId = $request->get('room_id');
        $this->servicesRoomViceCheck->delHighRoom($roomId);
        OutJson::outJsonNoData(Hint::SUCCESS);
    }


    /**
     * 消息回调服务
     *
     * 文档地址：https://doc.rongcloud.cn/imserver/server/v1/moderation/msgcb
     * @param Request $request
     */
    public function messageCallback(Request $request)
    {
        return $this->servicesRoomViceCheck->messageCallback($request->all());
    }

}
